1. /* slflladd.cpp by K.Tsuru */
  2. // function ID = 207 DRADIX, BRADIX
  3. /************************************************************************
  4. SLong and SInteger classes
  5. It provides the sum m+n (m and n have same sign).
  6. *************************************************************************/
  7. #ifndef SN_H
  8. #include "sn.h"
  9. #endif
  10. static const char* const func = "\'+\' or LLAdd";
  11. SLong LLAdd(const SLong& m, const SLong& n){
  12. if(m.Sign() == 0) return n;
  13. if(n.Sign() == 0) return m;
  14. if(m.Sign() != n.Sign()) m.SetError(m.SYNTAX_ERR, func, 207);
  15. //It lets m >= n. See DDMult().
  16. if( m.aHead < n.aHead ) return LLAdd(n, m);
  17. if(m.Radix() != n.Radix()) m.SetError(m.RADIX_ERR, func, 207);
  18. SLong result(m); // result = m
  19. const fType* nv = n.ReadFigures();
  20. fType* rv = result.figure.Elements();
  21. fType rdx = m.Radix();
  22. fType w = 0; //work variable 2*(radix-1) + 1 <= fTypeMax = 65535
  23. //addition upto top figure of n
  24. uint i;
  25. for(i = n.aTail; i <= n.aHead ; i++){
  26. w += rv[i] + nv[i];
  27. rv[i] = w % rdx;
  28. w = (w >= rdx) ? 1 : 0; // w /= rdx; w <= 1
  29. }
  30. uint hm = m.aHead;
  31. #ifndef NDEBUG
  32. result.figure(hm);
  33. #endif
  34. //When w=0 it is not executed.
  35. //result=m; Then the upper part of "result" is the same as that of m.
  36. //Except the special case such as 9999 9999 ... 9999+1 it ends by a few times.
  37. for( ; w && (i <= hm) ; i++){
  38. w += rv[i]; // nv[i] = 0
  39. rv[i] = w % rdx;
  40. w = (w >= rdx) ? 1 : 0; // w /= rdx; w <= 1
  41. }
  42. if(w){ //carry w <= 1
  43. hm++;
  44. #ifndef NDEBUG
  45. assert(w <= 1);
  46. #endif
  47. result.Reserve(hm);
  48. rv = result.figure.Elements();
  49. result.figure[hm] = w; //carry to the top figure
  50. }
  51. //It detects the figure position.
  52. result.aHead = hm;
  53. uint rt = min(m.Tail(), n.Tail()); // aTail --> Tail() since ver 2.191
  54. while(!rv[rt]) rt++;
  55. result.aTail = rt;
  56. result.SetSign(m.Sign());
  57. return result;
  58. }

slflladd.cpp : last modifiled at 2017/03/13 14:32:00(1,890 bytes)
created at 2017/10/07 10:26:50
The creation time of this html file is 2017/11/09 14:52:03 (Thu Nov 09 14:52:03 2017).